package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.bxscn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNFeeTemplate;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNFeeTemplateService;
import com.gzhryc.shared_device.bxscn.code.services.dto.BXSCNFeeTemplateSearch;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

public class BXSCNFeeTemplateController {

    @Route(value = { "/bxscn/fee_template/list.html" })
    public String list(HttpServletRequest request) {
        return "/bxscn/cost/fee_template/list.html";
    }

    @Route(value = "/bxscn/fee_template/list.data")
    public ListResult<BXSCNFeeTemplate> listData(HttpServletRequest request) {
        ListResult<BXSCNFeeTemplate> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        BXSCNFeeTemplateSearch search = WebUtils.getParameter(request, BXSCNFeeTemplateSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            search.setUserId(loginUser.getId());
            result.getBody().setTotal(BXSCNFeeTemplateService.self().countJoinBySearch(search));
            List<BXSCNFeeTemplate> feeTemplateList = BXSCNFeeTemplateService.self().findJoinBySearch(search, pageNo, pageLimit);
            if (feeTemplateList != null && feeTemplateList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(feeTemplateList);
            }
        }
        return result;
    }

    @Route(value = { "/bxscn/fee_template/add.html" })
    public String add(HttpServletRequest request) {
        return "/bxscn/cost/fee_template/edit.html";
    }

    @Route(value = {"/bxscn/fee_template/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增收费模版失败");
        BXSCNFeeTemplate feeTemplate = WebUtils.getParameter(request, BXSCNFeeTemplate.class);
        try {
            if(feeTemplate != null) {
                User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
                if (loginUser != null) {
                    feeTemplate.setUserId(loginUser.getId());

                    Double depositMoneyStr = WebUtils.getDouble(request, "depositMoneyStr");
                    if (depositMoneyStr == null || depositMoneyStr < 0.01 || depositMoneyStr > 99) {
                        result.setMsg("押金设置范围在0.01-99元");
                        return result;
                    }
                    feeTemplate.setDepositMoney(NumberTools.changeMoney(depositMoneyStr));

                    Double dayMaxMoneyStr = WebUtils.getDouble(request, "dayMaxMoneyStr");
                    if (dayMaxMoneyStr == null || dayMaxMoneyStr < 0 || dayMaxMoneyStr > 30) {
                        result.setMsg("日最大费用设置范围在0.01-30元");
                        return result;
                    }
                    feeTemplate.setDayMaxMoney(NumberTools.changeMoney(dayMaxMoneyStr));

                    Double payIntervalMoneyStr = WebUtils.getDouble(request, "payIntervalMoneyStr");
                    if (payIntervalMoneyStr == null || payIntervalMoneyStr < 0 || payIntervalMoneyStr > 10) {
                        result.setMsg("每小时支付金额设置范围在0-10元");
                        return result;
                    }
                    feeTemplate.setPayIntervalMoney(NumberTools.changeMoney(payIntervalMoneyStr));

                    if (BXSCNFeeTemplateService.self().add(feeTemplate)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/bxscn/fee_template/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        BXSCNFeeTemplate feeTemplate = BXSCNFeeTemplateService.self().getJoinById(id);
        WebUtils.setAttribute(request,feeTemplate);
        return "/bxscn/cost/fee_template/edit.html";
    }

    @Route(value = {"/bxscn/fee_template/update.do"})
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改收费模版失败");
        BXSCNFeeTemplate feeTemplate = WebUtils.getParameter(request, BXSCNFeeTemplate.class);
        try {
            if(feeTemplate != null) {
                User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
                if (loginUser != null) {
                    feeTemplate.setUserId(loginUser.getId());

                    Double depositMoneyStr = WebUtils.getDouble(request, "depositMoneyStr");
                    if (depositMoneyStr == null || depositMoneyStr < 0.01 || depositMoneyStr > 99) {
                        result.setMsg("押金设置范围在0.01-99元");
                        return result;
                    }
                    feeTemplate.setDepositMoney(NumberTools.changeMoney(depositMoneyStr));

                    Double dayMaxMoneyStr = WebUtils.getDouble(request, "dayMaxMoneyStr");
                    if (dayMaxMoneyStr == null || dayMaxMoneyStr < 0 || dayMaxMoneyStr > 30) {
                        result.setMsg("日最大费用设置范围在0.01-30元");
                        return result;
                    }
                    feeTemplate.setDayMaxMoney(NumberTools.changeMoney(dayMaxMoneyStr));

                    Double payIntervalMoneyStr = WebUtils.getDouble(request, "payIntervalMoneyStr");
                    if (payIntervalMoneyStr == null || payIntervalMoneyStr < 0 || payIntervalMoneyStr > 10) {
                        result.setMsg("每小时支付金额设置范围在0-10元");
                        return result;
                    }
                    feeTemplate.setPayIntervalMoney(NumberTools.changeMoney(payIntervalMoneyStr));

                    if (BXSCNFeeTemplateService.self().updateNotNull(feeTemplate)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = {"/bxscn/fee_template/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除收费模版失败");
        Long id = WebUtils.getLong(request,"id");
        Long total = PlaceBusinessConfigService.self().countByFeeTemplateId(EBusinessType.BXSCN.index(),id);
        if(total != null && total > 0){
            result.setMsg("该收费模版正在使用，不允许删除");
            return result;
        }
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            BXSCNFeeTemplate feeTemplate = BXSCNFeeTemplateService.self().getById(id);
            if(feeTemplate != null && loginUser.getId().equals(feeTemplate.getUserId())) {
                try {
                    if (BXSCNFeeTemplateService.self().deleteById(id)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    result.setMsg(e.getMessage());
                }
            }else {
                result.setCode(DtoConstants.NOT_POWER,"无权限删除");
            }
        }
        return result;
    }
}
